package com.qf.shop.controller;

import com.qf.shop.entity.Cart;
import com.qf.shop.entity.User;
import com.qf.shop.service.CartService;
import com.qf.shop.service.impl.CartServiceImpl;
import com.qf.shop.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


@WebServlet("/cart")
public class CartController extends BaseController {
    private CartService cartService = new CartServiceImpl();
    //添加购物车
    public String add(HttpServletRequest request, HttpServletResponse response){
        //判断是否登录  每个购物车都属于一个用户
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser==null){
            request.setAttribute("msg","添加购物车请先登录");
            return Constants.FORWARD+"/login.jsp";
        }
        //先获取当前登录用户的Id
        int id = loginUser.getId();
        //2.收参
        String gid  = request.getParameter("gid");
        //3.调用业务层  新增购物车数据
        int i = cartService.addCart(id,Integer.valueOf(gid));
        //4.成功跳转到cartSuccess.jsp 页面
        return Constants.FORWARD+"/cartSuccess.jsp";
    }
    //查询购物车
    public String show(HttpServletRequest request,HttpServletResponse response){
        //1.判断是否登录 ，每一个购物车属于一个用户
        HttpSession session = request.getSession();
        User loginUser = (User)session.getAttribute("loginUser");
        if(loginUser==null){
            request.setAttribute("msg","查询购物车请先登录！");
            return Constants.FORWARD+"login.jsp";
        }
        //先获取当前登录用户的ID
        int id  = loginUser.getId();
        //调用业务  查询当前用户的购物车
        List<Cart> carts = cartService.getCartById(id);
        request.setAttribute("carts",carts);
        return Constants.FORWARD+"/cart.jsp";
    }
    //删除购物车
    public String delete(HttpServletRequest request,HttpServletResponse response){
        //1.收参
        String gid = request.getParameter("gid");
        //2删除购物车数据
        cartService.deleteCartByGid(Integer.valueOf(gid));
        //3.跳转cart?method = show
        return  Constants.REDIRECT+"/cart?method=show";
    }
    //修改购物车
    public String update(HttpServletRequest request,HttpServletResponse response){
        //1.判断是否登录  每一个购物车都属于一个用户
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser==null){
            request.setAttribute("msg","修改购物车请先登录！");
            return Constants.FORWARD+"/login.jsp";
        }
        //先获取当前登录用的id
         int id = loginUser.getId();
        //收参
       String gid = request.getParameter("gid");
       String num = request.getParameter("num");
       String money = request.getParameter("money");
       Cart cart = new Cart(id,Integer.valueOf(gid),Integer.valueOf(num),Integer.valueOf(money));
       cartService.updateCart(cart);
       return null;
    }
    //清空购物车
    public String clear(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        cartService.clearCartById(Integer.valueOf(id));
        return Constants.REDIRECT+"/cart?method=show";
    }
}
